package com.dems.admin.config;

import com.dems.core.exception.FastDupException;
import com.dems.core.exception.LoanassistRuntimeException;
import com.dems.domain.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class FastExceptionHandler {
    /**
     * 拦截Exception类的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResponse<?> exceptionHandler(Exception e) {

        log.error("请求发生异常", e);

        if (e.getMessage().contains("Exception")) {
            return JsonResponse.fail();
        } else {
            return JsonResponse.failMsg(e.getMessage());
        }
    }


    @ExceptionHandler(LoanassistRuntimeException.class)
    @ResponseBody
    public JsonResponse<?> fastRuntimeExceptionHandler(LoanassistRuntimeException e) {

        log.error("请求发生异常", e);

        JsonResponse<?> response = JsonResponse.fail();
        //正常开发中，可创建一个统一响应实体，如CommonResp

        if (e.getErrorCode() != null) {
            response.setCode(e.getErrorCode().getCode());
        }

        response.setMessage(e.getMessage());
        return response;
    }

    @ExceptionHandler(FastDupException.class)
    @ResponseBody
    public JsonResponse<?> fastDupExceptionHandler(FastDupException e) {

        log.error("请求发生异常", e);

        JsonResponse<?> response = JsonResponse.dupFail();
        //正常开发中，可创建一个统一响应实体，如CommonResp
        response.setMessage(e.getMessage());
        return response;
    }

}
